#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS


#include <stdio.h>
#include <winsock2.h>
#pragma comment (lib, "ws2_32.lib")  //load ws2_32.dll

#define BUFF_SIZE 100

int main() {
	//init DLL
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);


	printf("wVersion: %d.%d\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
	printf("wHighVersion: %d.%d\n", LOBYTE(wsaData.wHighVersion), HIBYTE(wsaData.wHighVersion));
	printf("szDescription: %s\n", wsaData.szDescription);
	printf("szSystemStatus: %s\n", wsaData.szSystemStatus);

	//create sock
	SOCKET servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

	//bind sock
	struct sockaddr_in sockAddr;
	memset(&sockAddr, 0, sizeof(sockAddr));  //fill every element with 0
	sockAddr.sin_family = PF_INET;  //use IPv4 address
	sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");  //set speicific IP
	sockAddr.sin_port = htons(1234);  //port
	bind(servSock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));

	//listen
	listen(servSock, 20);

	//get request from client
	SOCKADDR clntAddr;
	int nSize = sizeof(SOCKADDR);

	while (1) {

		SOCKET clntSock = accept(servSock, (SOCKADDR*)&clntAddr, &nSize);
		// receive data from the client
		char buffer[BUFF_SIZE];
		int strLen = recv(clntSock, buffer, BUFF_SIZE, 0);

		//set data to client
		send(clntSock, buffer, strLen, NULL);

		memset(buffer, 0, BUFF_SIZE);

		closesocket(clntSock);
	}


	//close sock
	closesocket(servSock);


	//stop using dll
	WSACleanup();

	return 0;
}